package com.mycontact.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.mycontact.client.ClientFactory;
import com.mycontact.shared.ContactProxy;

public class ViewContactMainPanel extends Composite {
	private static MainPanelUiBinder uiBinder = GWT.create(MainPanelUiBinder.class);
	private ClientFactory clientFactory;
	private List<ContactProxy> CONTACTS;

	@UiField(provided = true) CellTable<ContactProxy> table;
	@UiField Label counter;

	interface MainPanelUiBinder extends UiBinder<Widget, ViewContactMainPanel> { }

	public ViewContactMainPanel() {
		createTable();
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public void setClientFactory(ClientFactory cf) {
		clientFactory = cf;
	}
	
	public void fillTable() {
		if(clientFactory != null) {
			clientFactory.getRequestFactory().contactRequest().generateContacts().fire(
					new Receiver<Void>() {
						@Override
						public void onSuccess(Void response) {
							updateTable();
						}
					}
			);
		}
	}
	
	private void updateTable() {
		if(clientFactory != null) {
			clientFactory.getRequestFactory().contactRequest().findAllContacts().fire(
					new Receiver<List<ContactProxy>>() {

						@Override
						public void onSuccess(List<ContactProxy> response) {
							//Set CONTACTS List
							CONTACTS = response;
							// Set the total row count
							table.setRowCount(CONTACTS.size(), true);
							// Push the data into the widget.
							table.setRowData(0, CONTACTS);
							// Set counter label
							counter.setText("Total of contacts: " + ((Integer)CONTACTS.size()).toString());
						}
						
					}
			);
		}		
	}

	private void createTable() {
		table = new CellTable<ContactProxy>();
		table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

		// Create name column
		TextColumn<ContactProxy> nameColumn = new TextColumn<ContactProxy>() {
			@Override
			public String getValue(ContactProxy object) {
				return object.getName();
			}
		};
		
		// Create address column
		TextColumn<ContactProxy> addressColumn = new TextColumn<ContactProxy>() {
			@Override
			public String getValue(ContactProxy object) {
				return object.getAddress();
			}
		};		
		
		table.addColumn(nameColumn, "Name");
		
		table.addColumn(addressColumn, "Address");

		// Add a selection model to handle user selection.
		final SingleSelectionModel<ContactProxy> selectionModel = new SingleSelectionModel<ContactProxy>();
		table.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						ContactProxy selected = selectionModel.getSelectedObject();
						if (selected != null) {
							clientFactory.setSelectedContactId(selected.getId().toString());
						}
					}
				});
	}

}
